//
//  IGRemoteScrollSetVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/25.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGRemoteScrollSetVC.h"
#import "IGScrollSetCell.h"
#import "LTPickerView.h"
#import "IGRemoteControlApi.h"

@interface IGRemoteScrollSetVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) IGEquRemoteModel *model;
@property (nonatomic, strong) NSArray *numArr;
@property (nonatomic, strong) IGRemoteControlApi *remoteApi;

@end

@implementation IGRemoteScrollSetVC

- (instancetype)initWithModel:(IGEquRemoteModel *)model {
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
//    self.title = @"轮播设置";
    self.title = NSLocalizedString(@"Loop Setting", nil);

    _numArr = @[@"0.5", @"1", @"2", @"5", @"10", @"30", @"60", @"120"];
    self.remoteApi = [IGRemoteControlApi new];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
    }
    return _tableView;
}

#pragma mark -<UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 2) {
        IGScrollSetCell *cell = [IGScrollSetCell cellWithTableView:tableView];
        __weak typeof(self) weakSelf = self;
        cell.gfSelect = [self.model.official boolValue];
        cell.zbSelect = [self.model.personal boolValue];
        cell.fxSelect = [self.model.share boolValue];
        cell.itemClickBlock = ^(IGRemoteControlBtn index, NSString *status) {
            weakSelf.remoteApi = [IGRemoteControlApi new];
            weakSelf.remoteApi.macId = weakSelf.model.macId;
            if (index == IGRemoteControlBtnGF) {
                weakSelf.remoteApi.official = status;
            } else if (index == IGRemoteControlBtnGR) {
                weakSelf.remoteApi.personal = status;
            } else if (index == IGRemoteControlBtnFX) {
                weakSelf.remoteApi.share = status;
            }

            __block IGRemoteControlBtn numIndex = index;
            [weakSelf.remoteApi startWithDecodeClass:[IGRemoteControlResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
                    //修改本地model的值
                    if (numIndex == IGRemoteControlBtnGF) {
                        weakSelf.model.official = [weakSelf.model.official boolValue]?@"0":@"1";
                    } else if (numIndex == IGRemoteControlBtnGR) {
                        weakSelf.model.personal = [weakSelf.model.personal boolValue]?@"0":@"1";;
                    } else if (numIndex == IGRemoteControlBtnFX) {
                        weakSelf.model.share = [weakSelf.model.share boolValue]?@"0":@"1";;
                    }
                    return;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableView reloadData];
                });
            }];
        };
        
        return cell;
    }
    static NSString *cellID = @"IGiGallerySetVCCellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    
    cell.textLabel.font = [UIFont font16];
    cell.textLabel.textColor = [UIColor c4Color];
    cell.detailTextLabel.textColor = [UIColor c5Color];
    cell.detailTextLabel.font = [UIFont font12];
    
    if (indexPath.row == 0) {
//        cell.textLabel.text = @"轮播开关";
        cell.textLabel.text = NSLocalizedString(@"Loop Button", nil);
        UISwitch *switchV = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        switchV.on = [self.model.loopOn boolValue];
        [switchV addTarget:self action:@selector(switChangeValueAction:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = switchV;
    } else if (indexPath.row == 1) {
//        cell.textLabel.text = @"轮播间隔";
        cell.textLabel.text = NSLocalizedString(@"Loop Interval", nil);
        //安全性校验 索引小于数组的个数+索引大于0
//        cell.detailTextLabel.text = (([self.model.carousel integerValue] - 1)<self.numArr.count && ([self.model.carousel integerValue] - 1)>=0)?[NSString stringWithFormat:@"%@分", [self.numArr objectAtIndex:([self.model.carousel integerValue] - 1)]]:@"未设置";
        cell.detailTextLabel.text = (([self.model.carousel integerValue] - 1)<self.numArr.count && ([self.model.carousel integerValue] - 1)>=0)?[NSString stringWithFormat:@"%@%@", [self.numArr objectAtIndex:([self.model.carousel integerValue] - 1)],NSLocalizedString(@"Min", nil)]:NSLocalizedString(@"Unset", nil);
    } else if (indexPath.row == 2) {
//        cell.textLabel.text = @"轮播内容";
        cell.textLabel.text = NSLocalizedString(@"Loop Content", nil);
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return 100.0;
    }
    return 50.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        LTPickerView* pickerView = [LTPickerView new];
        pickerView.dataSource = self.numArr;//设置要显示的数据
        //安全性校验 索引小于数组的个数+索引大于0
        pickerView.defaultStr = (([self.model.carousel integerValue] - 1)<self.numArr.count && ([self.model.carousel integerValue] - 1)>=0)?[self.numArr objectAtIndex:([self.model.carousel integerValue] - 1)]:[self.numArr objectAtIndex:0];//默认选择的数据
        [pickerView show];//显示
        //回调block
        __weak typeof(self) weakSelf = self;
        pickerView.block = ^(LTPickerView* obj,NSString* str,int num){
            weakSelf.remoteApi = [IGRemoteControlApi new];
            weakSelf.remoteApi.macId = self.model.macId;
            weakSelf.remoteApi.carousel = [NSString stringWithFormat:@"%@", @(num + 1)];
            [weakSelf.remoteApi startWithDecodeClass:[IGRemoteControlResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
                    weakSelf.model.carousel = [NSString stringWithFormat:@"%@", @(num + 1)];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf.tableView reloadData];
//                        [weakSelf presentMessageTips:@"修改成功"];
                        [weakSelf presentMessageTips:NSLocalizedString(@"Change Successfully", nil)];
                    });
                }
            }];
        };
    }
}

- (void)switChangeValueAction:(UISwitch *)switchV {
    self.remoteApi = [IGRemoteControlApi new];
    self.remoteApi.loopOn = switchV.on?@"1":@"0";
    self.remoteApi.macId = self.model.macId;
    __weak typeof(self) weakSelf = self;
    [self.remoteApi startWithDecodeClass:[IGRemoteControlResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            weakSelf.model.loopOn = [weakSelf.model.loopOn boolValue]?@"0":@"1";
        } else {
            switchV.on = !switchV.on;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
